package org.opensource.openapi3doc;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class OAS3Yaml {
    private static Logger logger = LoggerFactory.getLogger(OAS3Yaml.class);

    private ArrayList<String> yamlNameList = new ArrayList<String>();
    private Map<String, String> yamlFileMap = new HashMap<String, String>();

    private Environment env;

    // default value
    private static String yamlRootDir = "/opt/soft/microservices/openapi3-doc/yaml";

    public OAS3Yaml() {
    }

    public void setEnvironment(Environment env) {
        this.env = env;        
    }

    public void load() {
        logger.info("Load yaml from Spring boot root directory...");
        
        updateYamlRootDir();
        File dir = new File(yamlRootDir); 
        if (!dir.isDirectory()) {
            logger.error(yamlRootDir + " is not directory.");
            return;
        }

        String[] children = dir.list();
        for (String filename: children) {
            logger.info("file: " + filename + " is here.");
            if (!isYaml(filename))  {
                continue;
            }

            String name = parseMicroserviceName(filename);

            yamlNameList.add(name);
            yamlFileMap.put(name, "/" + filename);

            logger.info("Find YAML: " + name + ", Path: " + filename);
        }
        logger.info("Load yaml from Spring boot root directory ok");
    }

    private boolean isYaml(String filename) {
        if (filename.indexOf(".yml") > 0) {
            return true;
        }

        if  (filename.indexOf(".yaml") > 0) {
            return true;
        }

        return false;
    }

    private String parseMicroserviceName(String yamlName) {
        int i = yamlName.indexOf(".");
        return yamlName.substring(0, i);
    }

    public ArrayList<String> getYamlNameList() {
        return this.yamlNameList;
    }

    public String getYamlFile(String name) {
        return this.yamlFileMap.get(name);
    }

    private void updateYamlRootDir() {
        if (env == null) {
            logger.error("Environment autowired failedly, NULL.");
            return;
        }

        logger.info("========================================================================");
        String location = env.getProperty("spring.web.resources.static-locations");
        if (location == null || location.equals("")) {
            logger.error("No Config for spring.web.resources.static-locations");
            return;
        }

        int s = location.indexOf(":");
        if (s < 0) {
            yamlRootDir = location;
        }

        String str = location.substring(s + 1, location.length());
        if (str == null || str.length() == 0) {
            logger.error("Yaml root dir config is illegal.");
            return;
        }

        yamlRootDir = str;
        logger.info("Update YAML root dir to: " + yamlRootDir);
    }
}
